package com.telephone.purchase.service;

import com.telephone.purchase.mapper.IndexDaoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class IndexService {

    @Autowired
    IndexDaoMapper indexDaoMapper;

    public Map<String,String> getUnifiedInformation() {
        Map<String,String> map=new HashMap<>();
//        1.获取总注册用户
        int allUsers=indexDaoMapper.getAllUsers();
        map.put("allUsers",allUsers+"");
//        2.获取当天注册新用户
        int newUsers=indexDaoMapper.getNewUsers();
        map.put("newUsers",newUsers+"");
//        3.总订单数量
        int allOrders=indexDaoMapper.getAllOrders();
        map.put("allOrders",allOrders+"");
//        4.当天新订单数
        int newOrders=indexDaoMapper.getNewOrders();
        map.put("newOrders",newOrders+"");
//        5.VIP员人数
        int allAdmin=indexDaoMapper.getAllVIP();
        map.put("allAdmin",allAdmin+"");
//        6.当天新增VIP人数
        int newAdmin=indexDaoMapper.getNewVIP();
        map.put("newAdmin",newAdmin+"");
//        7.总交易额
        BigDecimal allMoney=indexDaoMapper.getAllMoney();
        map.put("allMoney",allMoney+"");
//        8.日交易额
        BigDecimal dayMoney=indexDaoMapper.getDayMoney();
        map.put("dayMoney",dayMoney+"");
        System.out.println("这是一个测试**************************");
        System.out.println(map);
        return map;
    }

    public Map<String,List> getPolyLineData() {
        Map<String,List> map=new HashMap<>();
//        1.前七月用户每月的人数
        List<Map<String, Object>> map1=indexDaoMapper.getUserByMonths();
        System.out.println("这是一个测试************************************");
        System.out.println(map1);
        Map<String, List> listMap1 = traverseMap(map1);
        System.out.println(listMap1);
        //获取时间信息
        List dateList = listMap1.get("dateList");
        //获取人数信息
        List userCount=listMap1.get("dataCount");
        map.put("dateList",dateList);
        map.put("userCount",userCount);
//        2.前7月订单每月的订单数
        List<Map<String, Object>> map2=indexDaoMapper.getOrderByMonths();
        System.out.println("这是一个测试************************************");
        System.out.println(map2);
        Map<String, List> listMap2 = traverseMap(map2);
        System.out.println(listMap2);
        List orderCount=listMap2.get("dataCount");
        map.put("orderCount",orderCount);
//        3.前七月每月的销售额
        List<Map<String, Object>> map3=indexDaoMapper.getMoneyByMonths();
        System.out.println("这是一个测试************************************");
        System.out.println(map3);
        Map<String, List> listMap3 = traverseMap(map3);
        System.out.println(listMap3);
        List moneyCount=listMap3.get("dataCount");
        map.put("moneyCount",moneyCount);
        return map;
    }
    /**
     * 对map数据进行封装方法
     * @param list
     * @return
     */
    public Map<String,List> traverseMap(List<Map<String, Object>> list){
        Map<String,List> data=new HashMap<>(2);
        List<String> dateList=new ArrayList<>(7);
        List<Object> dataCount=new ArrayList<>(7);
        for(Map map: list){
            System.out.println(map+"   "+map.size());
            Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry next = iterator.next();
                String key = (String) next.getKey();
                if("count".equals(key)){
                    dataCount.add( next.getValue());
                }else{
                    dateList.add((String) next.getValue());
                }
            }
        }
        data.put("dateList",dateList);
        data.put("dataCount",dataCount);
        return data;
    }

    public List<Map> getPieGraphData() {
        List<Map> list=new ArrayList<>();
        list=indexDaoMapper.getPieGraphData();
        return list;
    }
}
